using System;
using System.Net;
using newtelligence.DasBlog.Runtime;
using Subtext.Akismet;
using AkismetComment = Subtext.Akismet.Comment;

namespace newtelligence.DasBlog.Web.Core
{
    /// <summary>
    /// Uses the Akismet service to determine if a comment is SPAM
    /// </summary>
    /// <remarks>http://akismet.com/</remarks>
    public class AkismetSpamBlockingService : ISpamBlockingService
    {
        private readonly AkismetClient akismetClient;

        public AkismetSpamBlockingService(string apiKey, string blogUrl)
        {
            akismetClient = new AkismetClient(apiKey, new Uri(blogUrl));
        }

        #region ISpamBlockingService Members

        public bool IsSpam(IFeedback feedback)
        {
            IComment akismetFormattedComment = ConvertToAkismetComment(feedback);
            return akismetClient.CheckCommentForSpam(akismetFormattedComment);
        }

        public void ReportSpam(IFeedback feedback)
        {
            IComment akismetFormattedComment = ConvertToAkismetComment(feedback);
            akismetClient.SubmitSpam(akismetFormattedComment);
        }

        public void ReportNotSpam(IFeedback feedback)
        {
            IComment akismetFormattedComment = ConvertToAkismetComment(feedback);
            akismetClient.SubmitHam(akismetFormattedComment);
        }

        #endregion

        private static AkismetComment ConvertToAkismetComment(IFeedback feedback)
        {
            IPAddress ipAddress = IPAddress.None;
            if (feedback.AuthorIPAddress != null)
            {
                try
                {
                    ipAddress = IPAddress.Parse(feedback.AuthorIPAddress);
                }
                catch (FormatException)
                {
                }
            }

            AkismetComment comment = new AkismetComment(ipAddress, feedback.AuthorUserAgent);
            comment.Author = feedback.Author;
            comment.AuthorEmail = feedback.AuthorEmail;

            if (feedback.AuthorHomepage != null && feedback.AuthorHomepage.Length > 0)
            {
                try
                {
                    comment.AuthorUrl = new Uri(feedback.AuthorHomepage);
                }
                catch (UriFormatException)
                {
                }
            }

            comment.Content = feedback.Content;
            comment.Referer = feedback.Referer;

            if (feedback.TargetEntryId != null & feedback.TargetEntryId.Trim().Length > 0)
            {
                try
                {
                    comment.Permalink = new Uri(SiteUtilities.GetPermaLinkUrl(feedback.TargetEntryId));
                }
                catch (UriFormatException)
                {
                }
            }

            comment.CommentType = feedback.FeedbackType;

            return comment;
        }
    }
}